package com.sp2.dev.test.mongodb;

import com.mongodb.*;
import com.sp2.dev.test.hoteljxl.CsvVO;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-2-12
 * Time: 上午10:57
 * To change this template use File | Settings | File Templates.
 */
public class HotelMongodb extends MongodbConnection {
    private static final String tableName = "hotel";
    protected static DBCollection hotel;

    static {
        /**
         cmd>
         use test;
         db.createCollection("hotel");
         */
        hotel = db.getCollection(tableName);
    }

    @Test
    public void printHotel() {

        //查找
        DBCursor cursor = hotel.find().limit(100);
        int i = 0;
        while (cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            System.out.println(dbObject);
//            i++;
//            if (i == 100) break;
        }
        System.out.println(cursor.count());

//       System.out.println(cursor.getCursorId());
//        System.out.println(JSON.serialize(cursor));
    }

    /**
     * E:\zhimingli\hotel\最后5000.csv  共花费时间 5387
     * E:\zhimingli\hotel\1到200W.csv  共花费时间 244250
     * E:\zhimingli\hotel\200W到400W.csv  共花费时间 242770
     * E:\zhimingli\hotel\400W到600W.csv  共花费时间 275728
     * E:\zhimingli\hotel\600W到800W.csv  共花费时间 298288
     */
    public static void main(String[] args) {
        String filePath = "E:\\zhimingli\\hotel\\200W到400W.csv";
        File csv = new File(filePath); // CSV文件
        BufferedReader br = null;
        List<String> list = new ArrayList<String>();
        String line = "";
        Long start = System.currentTimeMillis();
        try {
            br = new BufferedReader(new FileReader(csv));
            while ((line = br.readLine()) != null) {
                CsvVO vo = getVo(line);
                if (vo == null) {
                    continue;
                }
                DBObject dbObject = reDBObject(vo);
                hotel.save(dbObject);

            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getStackTrace());
            e.getStackTrace();
        } catch (IOException io) {
            System.out.println(io.getStackTrace());
            io.getStackTrace();
        }
        Long end = System.currentTimeMillis();
        System.out.println(filePath + "  共花费时间 " + (end - start));
    }


    //模糊查询
    private BasicDBObject getLikeStr(String column, String findStr) {
        Pattern pattern = Pattern.compile("^.*" + findStr + ".*$", Pattern.CASE_INSENSITIVE);
        return new BasicDBObject(column, pattern);
    }


    @Test
    public void testLike() {
        //Pattern pattern = Pattern.compile("^王$", Pattern.CASE_INSENSITIVE); 完全匹配
        //Pattern pattern = Pattern.compile("^.*王$", Pattern.CASE_INSENSITIVE); 右匹配
        //Pattern pattern = Pattern.compile("^王.*$", Pattern.CASE_INSENSITIVE); 左匹配
        //Pattern pattern = Pattern.compile("^.*王.*$", Pattern.CASE_INSENSITIVE); 模糊匹配
        
        Pattern pattern = Pattern.compile("^.*" + "李志明" + ".*$", Pattern.CASE_INSENSITIVE);
        DBObject dbObject = new BasicDBObject("CName", pattern);
        DBCursor dbCursor = hotel.find(dbObject);
        while (dbCursor.hasNext()) {
            System.out.println(dbCursor.next());
        }
        System.out.println(dbCursor.count());
    }


    @Test
    public void findByName() {
        Long start = System.currentTimeMillis();
        String userName = "李志明";

        // hotel.remove();

        //创建一个
        //      db.createCollection("hotel",new BasicDBObject());

//        hotel.drop();


        DBCursor dbCursorTotle = hotel.find();
        System.out.println("总数是:" + dbCursorTotle.count());

        BasicDBObject dbObject = getLikeStr("CName", userName);

        DBCursor dbCursor = hotel.find(dbObject);

        while (dbCursor.hasNext()) {
            System.out.println(dbCursor.next());
        }
        Long end = System.currentTimeMillis();
        System.out.println(dbCursor.count());
        System.out.println("findByName" + userName + "  共花费时间 " + (end - start));
    }


    private static DBObject reDBObject(CsvVO vo) {
        DBObject dbObject = new BasicDBObject();
        dbObject.put("CName", vo.getCName());
        dbObject.put("CardNo", vo.getCardNo());
        dbObject.put("Descriot", vo.getDescriot());
        dbObject.put("CtfTp", vo.getCtfTp());
        dbObject.put("CtfId", vo.getCtfId());

        dbObject.put("Gender", vo.getGender());
        dbObject.put("Birthday", vo.getBirthday());
        dbObject.put("Address", vo.getAddress());

        dbObject.put("Zip", vo.getZip());
        dbObject.put("Dirty", vo.getDirty());
        dbObject.put("District1", vo.getDistrict1());

        dbObject.put("District2", vo.getDistrict2());
        dbObject.put("District3", vo.getDistrict3());
        dbObject.put("District4", vo.getDistrict4());

        dbObject.put("District5", vo.getDistrict5());
        dbObject.put("District6", vo.getDistrict6());
        dbObject.put("FirstNm", vo.getFirstNm());

        dbObject.put("LastNm", vo.getLastNm());
        dbObject.put("Duty", vo.getDuty());
        dbObject.put("Mobile", vo.getMobile());

        dbObject.put("Tel", vo.getTel());
        dbObject.put("Fax", vo.getFax());
        dbObject.put("EMail", vo.getEMail());

        dbObject.put("Nation", vo.getNation());
        dbObject.put("Taste", vo.getTaste());
        dbObject.put("Education", vo.getEducation());

        dbObject.put("Company", vo.getCompany());
        dbObject.put("CTel", vo.getCTel());
        dbObject.put("CAddress", vo.getCAddress());

        dbObject.put("CZip", vo.getCZip());
        dbObject.put("Family", vo.getFamily());
        dbObject.put("CVersion", vo.getCVersion());

        dbObject.put("id", vo.getId());
        return dbObject;
    }

    private static CsvVO getVo(String line) throws UnsupportedEncodingException {
        String arr[] = line.split(",");
        CsvVO vo = new CsvVO();
        if (arr.length > 0) {
            vo.setCName(arr[0].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 1) {
            vo.setCardNo(arr[1].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 2) {
            vo.setDescriot(arr[2].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 3) {
            vo.setCtfTp(arr[3].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 4) {
            vo.setCtfId(arr[4].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 5) {
            vo.setGender(arr[5].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 6) {
            vo.setBirthday(arr[6].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 7) {
            vo.setAddress(arr[7].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 8) {
            vo.setZip(arr[8].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 9) {
            vo.setDirty(arr[9].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 10) {
            vo.setDistrict1(arr[10].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 11) {
            vo.setDistrict2(arr[11].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 12) {
            vo.setDistrict3(arr[12].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 13) {
            vo.setDistrict4(arr[13].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 14) {
            vo.setDistrict5(arr[14].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 15) {
            vo.setDistrict6(arr[15].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 16) {
            vo.setFirstNm(arr[16].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 17) {
            vo.setLastNm(arr[17].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 18) {
            vo.setDuty(arr[18].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 19) {
            vo.setMobile(arr[19].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 20) {
            vo.setTel(arr[20].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 21) {

            vo.setFax(arr[21].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 22) {
            vo.setEMail(arr[22].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 23) {
            vo.setNation(arr[23].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 24) {
            vo.setTaste(arr[24].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 25) {
            vo.setEducation(arr[25].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 26) {
            vo.setCompany(arr[26].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 27) {
            vo.setCTel(arr[27].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 28) {
            vo.setCAddress(arr[28].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 29) {
            vo.setCZip(arr[29].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 30) {
            vo.setFamily(arr[30].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 31) {
            vo.setCVersion(arr[31].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        if (arr.length > 32) {
            vo.setId(arr[32].replaceAll("'", "").replaceAll("’", "").replaceAll("\\\\", ""));
        }
        return vo;
    }
}
